package cn.agiledata.bank.transaction.logic;

import java.math.BigDecimal;

import cn.agiledata.bank.transaction.exception.TransactionException;

/**
 * 批量导入交易
 * 
 * @author maxianbo
 * 
 */
public class BatchImportTrans {
    private String orderId;

    private BigDecimal amount;

    private String bankCode;

    private String bankName;

    private String accountName;

    private String accountNo;

    private BatchImportTrans() {
    }

    /**
     * 解析批量导入文件中的一行数据，生成批量导入交易
     * 
     * @param line
     *            数据行内容
     * @return 批量导入交易
     * @throws TransactionException
     *             如果解析过程出现错误
     */
    public static BatchImportTrans getInstance(String line)
            throws TransactionException {
        line = line.replace('|', '#'); // |不能用于分割，先替换为#
        String[] data = line.split("#");
        // 数据域数目
        if (data.length != 6) {
            throw TransactionException.getImportFileContentFormatException(
                    "FieldCount", data.length + "");
        }
        BatchImportTrans trans = new BatchImportTrans();

        // 订单编号
        String orderId = data[0].trim();
        if (orderId.length() == 0 || orderId.length() > 32) {
            if (orderId.length() == 0) {
                orderId = "empty";
            }
            throw TransactionException.getImportFileContentFormatException(
                    "orderId", orderId);
        }
        trans.orderId = orderId;

        // 金额
        try {
            trans.amount = new BigDecimal(data[1].trim());
            if (trans.amount.doubleValue() <= 0) {
                throw TransactionException.getImportFileContentFormatException(
                        "amount", data[1].trim());
            }
        } catch (NumberFormatException e) {
            throw TransactionException.getImportFileContentFormatException(
                    "amount", data[1].trim());
        }

        // 银行代码
        String bankCode = data[2].trim();
        if (bankCode.length() == 0) {
            throw TransactionException.getImportFileContentFormatException(
                    "bankCode", "empty");
        }
        trans.bankCode = bankCode.toLowerCase();

        // 开户行名称
        String bankName = data[3].trim();
        if (bankName.length() == 0) {
            throw TransactionException.getImportFileContentFormatException(
                    "bankName", "empty");
        }
        trans.bankName = bankName;

        // 账户名称
        String accountName = data[4].trim();
        if (accountName.length() == 0) {
            throw TransactionException.getImportFileContentFormatException(
                    "accountName", "empty");
        }
        trans.accountName = accountName;

        // 账号
        String accountNo = data[5].trim();
        if (accountNo.length() == 0) {
            throw TransactionException.getImportFileContentFormatException(
                    "accountNo", "empty");
        }
        trans.accountNo = accountNo;

        return trans;
    }

    /**
     * 
     * @return 订单编号
     */
    public String getOrderId() {
        return this.orderId;
    }

    /**
     * 
     * @return 金额
     */
    public BigDecimal getAmount() {
        return this.amount;
    }

    /**
     * 
     * @return 银行代码
     */
    public String getBankCode() {
        return this.bankCode;
    }

    /**
     * 
     * @return 开户行名称
     */
    public String getBankName() {
        return this.bankName;
    }

    /**
     * 
     * @return 账户名称
     */
    public String getAccountName() {
        return this.accountName;
    }

    /**
     * 
     * @return 账号
     */
    public String getAccountNo() {
        return this.accountNo;
    }
}
